import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart' as getx;
import 'package:get/get_core/src/get_main.dart';
import 'package:grocery_app_demo/routes/app_routes.dart';

class ResponseInterceptor extends Interceptor {
  @override
  void onResponse(
    Response response,
    ResponseInterceptorHandler handler,
  ) {
    var code = response.data['code'];
    if (code != null) {
      if (code == 0) {
        handler.next(response);
      } else if (code == 401) {
        getx.Get.snackbar("登陆信息已过期", "请重新登陆", colorText: Colors.red);
        Get.offAllNamed(AppRoutes.loginPage);
      } else {
        var msg = response.data['msg'];
        getx.Get.snackbar("请求失败", msg, colorText: Colors.red);
      }
    }
  }
}
